import uuid
from django.db import models
from apps.users.models import PlatformUser


# Create your models here.
class CrawlerConfig(models.Model):
    task_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, verbose_name='任务ID')
    platform_name = models.CharField(max_length=50, verbose_name='平台名称')
    crawler_type = models.CharField(max_length=30, verbose_name='爬取类型')
    keyword = models.CharField(max_length=50, verbose_name='关键字', null=True, blank=True)
    id_list = models.CharField(max_length=1000, blank=True, null=True, verbose_name='ID列表')
    creator_list = models.CharField(max_length=1000, blank=True, null=True, verbose_name='创作者列表')
    remark = models.CharField(max_length=256, blank=True, null=True, verbose_name='备注')
    is_crawler_comment = models.BooleanField(default=False, verbose_name='是否爬取评论')
    is_crawler_sub_comment = models.BooleanField(default=False, verbose_name='是否爬取二级评论')
    is_proxy_enabled = models.BooleanField(default=False, verbose_name='是否开启IP代理')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    user = models.ForeignKey(PlatformUser, on_delete=models.CASCADE, related_name='crawler_config', verbose_name='配置用户', default=1)

    class Meta:
        verbose_name = '爬虫配置'
        verbose_name_plural = '爬虫配置信息'


class CrawlerStatus(models.Model):
    crawler_status = models.CharField(max_length=50, verbose_name='爬虫状态')
    start_time = models.DateTimeField(verbose_name='开始爬虫时间', auto_now_add=True)
    end_time = models.DateTimeField(verbose_name='完成爬虫时间', null=True, blank=True)
    crawler_status_logs = models.TextField(verbose_name='爬虫状态日志', null=True, blank=True)
    task_id = models.ForeignKey(
        CrawlerConfig,
        db_column='task_id',
        on_delete=models.CASCADE,
        related_name='statuses',
        verbose_name='任务'
    )

    class Meta:
        verbose_name = '爬虫状态'
        verbose_name_plural = '爬虫状态列表'


class CrawlerCookiesAccount(models.Model):
    account_name = models.CharField(max_length=64, default='', verbose_name='账号名称')
    platform_name = models.CharField(max_length=64, default='', verbose_name='平台名称 (xhs | dy | ks | wb | bili | tieba)')
    cookies = models.TextField(verbose_name='对应自媒体平台登录成功后的cookies')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='该条记录的创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='该条记录的更新时间')
    invalid_timestamp = models.BigIntegerField(default=0, verbose_name='账号失效时间戳')
    status = models.SmallIntegerField(default=0, verbose_name='账号状态枚举值(0：有效，-1：无效)')
    user = models.ForeignKey(PlatformUser, on_delete=models.CASCADE, related_name='crawler_cookies_account', verbose_name='配置用户', default=1)

    class Meta:
        db_table = 'crawler_cookies_account'
        verbose_name = '爬虫采集账号表（cookies）'
        indexes = [
            models.Index(fields=['update_time'], name='idx_crawler_cookies_account_01'),
        ]


class ProxyConfig(models.Model):
    provider = models.CharField(max_length=64, default='kuaidaili', verbose_name='代理商名称')
    username = models.CharField(max_length=64, verbose_name='用户名', null=True, blank=True)
    password = models.CharField(max_length=64, verbose_name='密码', null=True, blank=True)
    secret_id = models.CharField(max_length=64, verbose_name='Secret ID', null=True, blank=True)
    secret_key = models.CharField(max_length=64, verbose_name='Secret Key', null=True, blank=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    status = models.SmallIntegerField(default=0, verbose_name='状态枚举值(0：有效，-1：无效)')
    user = models.ForeignKey(PlatformUser, on_delete=models.CASCADE, related_name='proxy_config', verbose_name='配置用户', default=1)

    class Meta:
        db_table = 'proxy_config'
        verbose_name = '代理配置表'
        indexes = [
            models.Index(fields=['update_time'], name='idx_proxy_config_01'),
        ]
